package com.ncloud.chnl.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.ncloud.chnl.business.ChnlBusiness;
import com.ncloud.chnl.common.Constant;
import com.ncloud.chnl.entity.Chnl;
import com.ncloud.chnl.mapper.ChnlMapper;
import com.ncloud.chnl.service.ChnlService;
import com.ncloud.common.enmus.ResultEnum;
import com.ncloud.common.exception.UserException;
import com.ncloud.common.utils.PageHelperUtils;
import com.ncloud.param.service.SequenceService;


@Service
public class ChnlServiceImpl implements ChnlService {
	
	private static final Logger logger = LogManager.getLogger(ChnlServiceImpl.class);
	
	@Autowired
	private ChnlMapper chnlMapper;
	@Reference
	private SequenceService sequence;
	@Autowired
	private ChnlBusiness chnlBusiness;

	@Override
	public Object deleteChnl(JSONObject json) {
		Long chnlId = json.getLong("chnlId");
		try {
			Long userId = json.getLong("userID");
			Chnl chnl = getChnlByPk(chnlId);
			chnl.setState(Constant.CHNL_COMMON_STATE_DELETE);
			chnl.setCreateTime(new Date());
			chnl.setUpdateTime(new Date());
			chnl.setCreateUser(userId);
			chnl.setUpdateUser(userId);
			chnlMapper.deleteChnl(chnl);
		} catch (RuntimeException e) {
			e.printStackTrace();
			throw new UserException(ResultEnum.DATABASE_ERROR);
		}
		return HttpStatus.OK;
	}
	
	private Chnl getChnlByPk(Long chnlId){
		return chnlMapper.getChnl(chnlId);
	}

	@Override
	public JSONObject getChnl(JSONObject json) {
		Chnl chnl = JSON.parseObject(json.toJSONString(),Chnl.class);
		if(chnl.getChnlId()==null){
			throw new UserException("2001", "传入参数有误");
		}
		chnl = getChnlByPk(chnl.getChnlId());
		return JSON.parseObject(JSON.toJSONString(chnl));
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public Object getChnlList(JSONObject json) {
		PageInfo pageInfo =null;
		Object object = null;
		Long userId = json.getLong("userID");
		String chnlLevel = "1";
		Long chnlId = json.containsKey("chnlId")?json.getLong("chnlId"):0L;
		if(PageHelperUtils.isPage(json)){
			PageHelperUtils.setPageParam(json);
			List<Chnl> chnls = chnlMapper.getChnlList(userId,chnlId,chnlLevel);
			pageInfo = new PageInfo(chnls);
			logger.debug("分页信息："+ JSON.toJSONString(pageInfo));
			object = JSON.parseObject(JSON.toJSONString(pageInfo));
		}else{
			List<Chnl> chnls = chnlMapper.getChnlList(userId,chnlId,chnlLevel);
			object = JSON.parseArray(JSON.toJSONString(chnls));
		}
		
		return object;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public Object getChnlGroupList(JSONObject json) {
//		if(!json.containsKey("chnlId") || json.getLong("chnlId")==null){
//			throw new UserException(ResultEnum.PARAM_ERROR);
//		}
		PageInfo pageInfo =null;
		Object object = null;
		Long userId = json.getLong("userID");
		String chnlLevel = null;
		Long chnlId = json.containsKey("chnlId")?json.getLong("chnlId"):0L;
		if(PageHelperUtils.isPage(json)){
			PageHelperUtils.setPageParam(json);
			List<Chnl> chnls = chnlMapper.getChnlList(userId,chnlId,chnlLevel);
			pageInfo = new PageInfo(chnls);
			logger.debug("分页信息："+ JSON.toJSONString(pageInfo));
			object = JSON.parseObject(JSON.toJSONString(pageInfo));
		}else{
			List<Chnl> chnls = chnlMapper.getChnlList(userId,chnlId,chnlLevel);
			object = JSON.parseArray(JSON.toJSONString(chnls));
		}
		
		return object;
	}

	@Override
	public JSONObject getChnlByUserId(JSONObject json) {
		if(!json.containsKey("userID"))
			throw new UserException("2001", "传入参数有误");
		Long userId = json.getLong("userID");
		Chnl chnl = chnlMapper.getChnlByUserId(userId);
		return JSON.parseObject(JSON.toJSONString(chnl));
	}

	@Override
	public JSONObject getChnlByUserId(Long userId) {
		Chnl chnl = chnlMapper.getChnlByUserId(userId);
		return JSON.parseObject(JSON.toJSONString(chnl));
	}

	@Override
	public Object insertChnl(JSONObject json) {
		if(!json.containsKey("chnlName") || !json.containsKey("parentId"))
			throw new UserException(ResultEnum.PARAM_ERROR);
		chnlBusiness.insertChnl(json);
		return HttpStatus.CREATED;
	}
	
	@Override
	public Object updateChnl(JSONObject json) {
		if (!json.containsKey("chnlId"))
			throw new UserException(ResultEnum.PARAM_ERROR);
		Chnl chnlInfo = new Chnl();
		Long chnlId = json.getLong("chnlId");
		if (chnlId == null) {
			throw new UserException("传入渠道编码不存在！");
		} else {
			chnlInfo = chnlMapper.getChnl(chnlId);
		}
		if (json.containsKey("chnlName") && StringUtils.isNotEmpty(json.getString("chnlName"))) {
			String chnlName = json.getString("chnlName");
			chnlInfo.setChnlName(chnlName);
		}
		if (json.containsKey("chnlType") && StringUtils.isNotEmpty(json.getString("chnlType"))) {
			String chnlType = json.getString("chnlType");
			chnlInfo.setChnlType(chnlType);
		}
		if (json.containsKey("parentId")) {
			Long parentId = json.getLong("parentId");
			chnlInfo.setParentId(parentId);
		}
		if (json.containsKey("updateUser")) {
			Long updateUser = json.getLong("updateUser");
			chnlInfo.setUpdateUser(updateUser);
		}
		if (json.containsKey("remark")) {
			String remark = json.getString("remark");
			chnlInfo.setRemark(remark);
		}
		if(json.containsKey("province")){
			String province =json.getString("province");
			chnlInfo.setProvince(province);
		}
		if(json.containsKey("area")){
			String area =json.getString("area");
			chnlInfo.setArea(area);
		}
		if(json.containsKey("address")){
			String address =json.getString("address");
			chnlInfo.setAddress(address);
		}
		if(json.containsKey("enterpriseLegal")){
			String enterpriseLegal =json.getString("enterpriseLegal");
			chnlInfo.setEnterpriseLegal(enterpriseLegal);
		}
		if(json.containsKey("legalCertNum")){
			String legalCertNum =json.getString("legalCertNum");
			chnlInfo.setLegalCertNum(legalCertNum);
		}
		if(json.containsKey("contact")){
			String contact =json.getString("contact");
			chnlInfo.setContact(contact);
		}
		if(json.containsKey("email")){
			String email =json.getString("email");
			chnlInfo.setEmail(email);
		}
		if(json.containsKey("phone")){
			String phone =json.getString("phone");
			chnlInfo.setPhone(phone);
		}
		if(json.containsKey("fax")){
			String fax =json.getString("fax");
			chnlInfo.setFax(fax);
		}

		try {
			chnlMapper.updateChnl(chnlInfo);
		} catch (RuntimeException e) {
			e.printStackTrace();
			throw new UserException(ResultEnum.DATABASE_ERROR);
		}
		return HttpStatus.OK;
	}

	@Override
	public JSONArray getAllChnl(JSONObject json) {
		List<Chnl> chnls = chnlMapper.getAllChnl();
		return JSON.parseArray(JSON.toJSONString(chnls));
	}
	
}
